package no.ntnu.item.ttm4115.termproject.socialmatchingrole_playeragent;


import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import com.thoughtworks.xstream.XStream;
import no.ntnu.item.ttm4115.termproject.extrasagent.Location;
import no.ntnu.item.ttm4115.termproject.SocialPlayerSig;
import no.ntnu.item.ttm4115.termproject.StartSmPa;
import no.ntnu.item.ttm4115.termproject.extrasagent.Group;
import no.ntnu.item.ttm4115.termproject.extrasagent.Player;
import no.ntnu.item.ttm4115.termproject.extrasagent.Preferences;
import no.ntnu.item.ttm4115.termproject.playeragent.PlayerAgentSM;
import no.ntnu.item.ttm4115.termproject.playeragent.gui.COLGUI;
import no.ntnu.item.ttm4115.termproject.playeragent.gui.LO;
import no.ntnu.item.ttm4115.termproject.socialmatchingrole_playeragent.gui.SocialMatchingTab;
import no.ntnu.item.ttm4115.termproject.Matched_Player_Response;
import no.tellu.common.javaframe.ActorAddress;
import no.tellu.common.javaframe.messages.RoleRequestMsg;
import no.ntnu.item.ttm4115.termproject.GroupSignal;
import no.ntnu.item.ttm4115.termproject.UpdateStatusToPlayers;
import no.ntnu.item.ttm4115.termproject.SendGroupPlayersToPA;
import no.ntnu.item.ttm4115.termproject.FeedBack;
import no.ntnu.item.ttm4115.termproject.PlayerUpdateFromPA;
import no.ntnu.item.ttm4115.termproject.GroupMatch;


public class SocialMatchingRole_PlayerAgentActions extends Object {
	public static void wait(SocialMatchingRole_PlayerAgentSM asm){
	}

	public static void createSMtab(StartSmPa signal, SocialMatchingRole_PlayerAgentSM asm){
	    // TODO edit action statements
		asm.mainFrame = signal.mainFrame;
		asm.smTab = new SocialMatchingTab(asm.mainFrame, asm.mainFrame.getPlayer(), asm);
		asm.mainFrame.getPane().addTab("Social Matching", asm.smTab);	
/*		asm.colTab =  new COLGUI();
		LO test = new LO("Quiz",0);
		asm.colTab.InsertLO(test);
		asm.mainFrame.getPane().addTab("COL",asm.colTab);
*/		asm.sendMessage(new RoleRequestMsg(), new ActorAddress());
		
	}

	public static void Receive_matched_players(Matched_Player_Response signal, SocialMatchingRole_PlayerAgentSM asm){
	 
		//store updated player profile 
		//and redraw match user list in GUI
		
		XStream xstream = new XStream();
   	  	xstream.setClassLoader(new Player().getClass().getClassLoader());
   	  	xstream.alias("player",Player.class);
   	  	xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES);
		 	  
		Object obj = (Object)xstream.fromXML(signal.mfao);
		Player lol = (Player)obj; 
		 	
		for(int i= 0; i< lol.getMatchList().size(); i++) System.out.println("Match["+ i+"] ="+ lol.getMatchList().get(i));
		
	
		asm.myPlayer =lol;
		asm.smTab.drawUserList(lol);
		
	}

		
	public static void updatePosition(no.ntnu.item.ttm4115.termproject.Location signal, SocialMatchingRole_PlayerAgentSM asm){
	    // TODO edit action statements
			Iterator<Map.Entry> playerit = ((PlayerAgentSM)((SocialMatchingRole_PlayerAgentSM)asm).mainFrame.getAsm()).onlinePlayersMap.entrySet().iterator();
			Player tmp = new Player();
			
			
			// find player in some map
			while(playerit.hasNext()){
				tmp = (Player) playerit.next().getValue();
				if(signal.getSenderRole().getActorID().equals(tmp.getActorAddress().getActorID()+"/socialMatchingRole_PlayerAgent")){
					System.out.println("Location update from: "+ tmp.getUserName());
					tmp.setlocation(new Location(signal.lat,signal.lng));
					break;
				}
			}
			
			XStream xstream = new XStream();
	   	  	xstream.setClassLoader(new Player().getClass().getClassLoader());
	   	  	xstream.alias("player",Player.class);
	   	  	xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES);
	
	   	  	String mfao = xstream.toXML(tmp);
			asm.sendMessage(new SocialPlayerSig(mfao),new ActorAddress("/classManager/socialClassManager","SocialMatchingRole_ClassManager"));

	}

	public static void updatePreferences(no.ntnu.item.ttm4115.termproject.Preferences signal, SocialMatchingRole_PlayerAgentSM asm){
		
				Iterator<Map.Entry> playerit = ((PlayerAgentSM)((SocialMatchingRole_PlayerAgentSM)asm).mainFrame.getAsm()).onlinePlayersMap.entrySet().iterator();
				Player tmp = new Player();
				// find player in online player map & edit
				while(playerit.hasNext()){
					tmp = (Player) playerit.next().getValue();
					if(signal.getSenderRole().getActorID().equals(tmp.getActorAddress().getActorID()+"/socialMatchingRole_PlayerAgent")){
						System.out.println("Preferences update from "+ tmp.getUserName());
						tmp.setPreferences(new Preferences(signal.name,signal.address,signal.gender,signal.dob,signal.interests));
						break;
					}
				}
			
				XStream xstream = new XStream();
		   	  	xstream.setClassLoader(new Player().getClass().getClassLoader());
		   	  	xstream.alias("player",Player.class);
		   	  	xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES);
	
		   	  	String mfao = xstream.toXML(tmp);
				asm.sendMessage(new SocialPlayerSig(mfao),new ActorAddress("/classManager/socialClassManager","SocialMatchingRole_ClassManager"));

	}

	public static void updateStatus(UpdateStatusToPlayers signal, SocialMatchingRole_PlayerAgentSM asm){
	    
		//piped signal from playeragent
		//update locally stored version instead
		
		XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		Object object = (Object)xstream.fromXML(signal.xmlPlayer);
		Player pl = (Player) object;
		for(int i=0; i< asm.myPlayer.getMatchList().size(); i++){
			if(asm.myPlayer.getMatchList().get(i).getUserName().equals(pl.getUserName())) {
				asm.myPlayer.getMatchList().set(i,pl);
				break;
			}	
		}	
		asm.smTab.drawUserList(asm.myPlayer);
		
	}

	public static void handleGroupUpdate(PlayerUpdateFromPA signal, SocialMatchingRole_PlayerAgentSM asm){
	    		XStream xstream = new XStream();
				xstream.setClassLoader(new Player().getClass().getClassLoader());
				xstream.alias("player", Player.class);
				xstream.alias("group", Group.class);
				xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
				
				Object object = (Object)xstream.fromXML(signal.xmlPlayer);
				Player pl = (Player) object;
				
				
				for(int i=0; i< pl.getGroups().size(); i++){
					boolean isthere = false;
					for(int h =0; h< asm.myPlayer.getGroups().size(); h++){
						if(pl.getGroups().get(i).getGroupName().equals("RedGroup")) isthere = true;
						else if(pl.getGroups().get(i).getGroupName().equals(asm.myPlayer.getGroups().get(h).getGroupName())){
							isthere = true;
						}
						
					}	
					System.out.println("RECEIVED SOCIAL GROUPS "+pl.getGroups().get(i).getGroupName());	
					if(!isthere){asm.myPlayer.addGroup(pl.getGroups().get(i));}
				}
				
				String mfao = xstream.toXML(asm.myPlayer);
				asm.sendMessage(new GroupSignal(mfao),new ActorAddress("/classManager/socialClassManager","SocialMatchingRole_ClassManager") );

	}

	public static void putSocialGroups(GroupMatch signal, SocialMatchingRole_PlayerAgentSM asm){
		XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		Object object = (Object)xstream.fromXML(signal.xmlPlayer);
		asm.myPlayer = (Player) object;
		
		asm.socialGroups.clear();
		
		for(int i=0; i< asm.myPlayer.getMatchList().size(); i++){
			for(int j =0; j< asm.myPlayer.getMatchList().get(i).getGroups().size(); j++){
				asm.socialGroups.add(asm.myPlayer.getMatchList().get(i).getGroups().get(j));
				
			}	
		}
		
		
		
		
	}



}
